package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Page;
import com.itheima.entity.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        long employee = (long) session.getAttribute("employee");
        setmealDto.setCreateUser(employee);
        setmealDto.setUpdateUser(employee);

        setmealService.save(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> Page(int page,int pageSize,String name){
      Page<SetmealDto> setmealDtoPage=  setmealService.page(page,pageSize,name);
      return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(long categoryId,Integer status){
      List<Setmeal> setmealList=  setmealService.list(categoryId,status);
      return R.success(setmealList);
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.status(status,ids);
        return R.success("状态修改成功");

    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> get(@PathVariable("id") long id){
       SetmealDto setmealDto= setmealService.findById(id);
       return R.success(setmealDto);
    }

    @PutMapping
    public  R<String> updateSetmael(@RequestBody SetmealDto setmealDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);

        setmealService.updateWithCategory(setmealDto);
        return R.success("修改套餐成功");
    }


}
